<?php
class downloadMod extends base{
	/**
	 * 列表页
	 *
	 */
	public function index(){
		$db = model('download');
		//分页
		$page=new Page();
		$page->pageSuffix=cpConfig::get('URL_HTML_SUFFIX');
		$listRows=40;//每页显示的信息条数
		$cur_page=$page->getCurPage();
		$limit_start=($cur_page-1)*$listRows;
		$limit=$limit_start.','.$listRows;
		
		//信息总数
		$count = $db->count();
		//当页信息列表
		$result = $db->limit($limit)->order('id desc')->select();

		$array = array(
		'list'=>$result,
		);
		$this->assign($array);
		
		$this->display();
	}
	/**
	 * 添加界面
	 *
	 */
	public function add(){		
		if ($_POST) {
			$this->do_add();
			exit;
		}
		
		$this->display();
	}
	/**
	 * 添加操作
	 *
	 */
	public function do_add(){		
		//模型
		$db = model('download');
		
		$data = $db->checkData();
		$error = $db->getError();
		if ($error) {
			$this->error($error);
		}
		$data['file'] = $this->_upFile();
		if(!$data['file']){
			$this->error("请选择要上传的文件");
		}
		
		$data['uid'] = $this->_G['member']['uid'];
		$id = $db->data($data)->insert();
		if ($id) {		
			$this->success('发布成功',url('download/index'));
		}else {
			$this->error('添加失败，请重新添加');
		}	
	}
	/**
	 * 删除
	 *
	 */
	public function del(){		
		$id = intval($_GET['id']);
		if ($id) {
			$del[] = $id;
		}else {
			$del = $_POST['id'];
		}
		$db = model('download');
		if ($del) {
			foreach ($del as $val){
				//$file=$db->where("id=$val")->find();
				//unlink($file['file']);
				$db->where("id=$val")->delete();
			}
		}
		$this->success('删除完毕');
	}
	/**
	 * 下载
	 *
	 */
	public function downfile(){		
		$id = intval($_GET['id']);
		
		$db = model('download');
		$down=$db->where("id=".$id)->find();
		
		if(!$down) $this->error('没有找到你要下载的文件');
		
		
		$ua = $_SERVER["HTTP_USER_AGENT"];
		
		$down['file'] =iconv ( 'UTF-8', 'GBK', $down['file'] );//转码法中文出错		
		$filename = basename($down['file']);	
		
		$file = fopen( $down['file'] ,"r");

		header('Content-Type: application/octet-stream');
		header("Accept-Ranges: bytes");
        header("Accept-Length: ".filesize($down['file']));
		if (preg_match("/MSIE/", $ua)) {
			//$encoded_filename = urlencode($filename);
			//$encoded_filename = str_replace("+", "%20", $encoded_filename);
			header('Content-Disposition: attachment; filename="' . $filename  . '"');
		} else if (preg_match("/Firefox/", $ua)) {
			header('Content-Disposition: attachment; filename*="utf8\'\'' . $filename . '"');
		} else {
			header('Content-Disposition: attachment; filename="' . $filename . '"');
		}
		
        echo fread($file, filesize($down['file']));
		$update['download'] = $down['download']+1;
		$db->where('id='.$id)->data($update)->update();
        fclose($file);

	}
	
	
	
	//图片上传
	private function _upFile(){

		/*上传文件处理*/
		$upload = new UploadFile();// 实例化上传类

		$upload->maxSize  = 3145728 ;// 设置附件上传大小
		$upload->allowExts  = array('xls', 'xlsx');// 设置附件上传类型

		$upload->saveRule = str_replace('.xlsx','',str_replace('.xls','',$_FILES['file']['name'])); //生成文件名称
		$upload->savePath =  'upfile/download/';// 设置附件上传目录
		
		if(!$upload->upload()) {// 上传错误提示错误信息
			$error = $upload->getErrorMsg();
			$this->error($error);
			return false;
		}else{// 上传成功
			$excel=$upload->getUploadFileInfo();			
			return $excel['file']['savepath'].$excel['file']['savename'];

		}
		/*上传文件处理结束*/
	}
	
}